package cn.fzkj.springboot.common;

/**
 * @DESCRIPTION 统一返回格式封装
 */
public class ResultData<T> {
    private String status;
    private String message;
    private T data;
    private Long timestamp;

    public ResultData(){
        this.timestamp = System.currentTimeMillis();
    }

    public ResultData(ResultCode code){
        this.status = code.getStatus();
        this.message = code.getMessage();
        this.timestamp = System.currentTimeMillis();
    }

    public static ResultData success(){
        return new ResultData<>(ResultCode.RC200);
    }

    public static <T> ResultData<T> success(T data){
        ResultData<T> result = new ResultData<>(ResultCode.RC200);
        result.setData(data);
        return result;
    }

    public static ResultData fail(){
        return new ResultData(ResultCode.RC100);
    }

    public static ResultData fail(String code, String message){
        ResultData resultData = new ResultData();
        resultData.setStatus(code);
        resultData.setMessage(message);
        return resultData;
    }

    public static <T> ResultData<T> fail(T data){
        ResultData<T> result = new ResultData<>(ResultCode.RC100);
        result.setData(data);
        return result;
    }

    /** ========================================================= */

    public void setStatus(String status){
        this.status = status;
    }
    public String getStatus(){
        return this.status;
    }
    public void setMessage(String message){
        this.message = message;
    }
    public String getMessage(){
        return this.message;
    }
    public void setData(T data){
        this.data = data;
    }
    public T getData(){
        return this.data;
    }
    public Long getTimestamp(){
        return this.timestamp;
    }
    public void setTimestamp(Long timestamp){
        this.timestamp = timestamp;
    }
}
